﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HR.Appointments.Service;

public partial class Config : EmployeePage
{
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckCredentials();
        _imgConfirm.Visible = IsLoggedIn;
        if (!IsPostBack)
        {
            ListWorkstations();
            ListActivity();
        }
    }

    //Bind the confimation button to each row
    protected void _dtgAction_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("_lnkActionDelete");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm('Viltu örugglega eyða?')"); // +
        }
    }
    protected void _dtgAction_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		System.Collections.Specialized.OrderedDictionary keys =
			(System.Collections.Specialized.OrderedDictionary)_dtgAction.DataKeys[e.RowIndex].Values;

		int companyId = Convert.ToInt32(keys["CompanyId"]);
		int activityId = Convert.ToInt32(keys["ActivityId"]);

        try
        {
            Facade.Employee().DeleteActivity(companyId, activityId);
        }
        catch (SubmitException)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að eyða.');", true);
        }

		ListActivity();
	}

    protected void _dtgAction_RowEditing(object sender, GridViewEditEventArgs e)
    {
        System.Collections.Specialized.OrderedDictionary keys =
            (System.Collections.Specialized.OrderedDictionary)_dtgAction.DataKeys[e.NewEditIndex].Values;

        //int companyId = Convert.ToInt32(keys["CompanyId"]);
        int activityId = Convert.ToInt32(keys["ActivityId"]);

        Response.Redirect(string.Format("~/NewEntryAction.aspx?Change=true&ActivityId={0}", activityId));
    }

    //Bind the confimation button to each row
    protected void _dtgWorkstation_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("_lnkWorkstaionDelete");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm('Viltu örugglega eyða?')"); // +
            //DataBinder.Eval(e.Row.DataItem, "WorkstationID") + "')");
        }
    }

	protected void _dtgWorkstation_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
        System.Collections.Specialized.OrderedDictionary keys =
            (System.Collections.Specialized.OrderedDictionary)_dtgWorkstation.DataKeys[e.RowIndex].Values;

        int companyId = Convert.ToInt32(keys["CompanyId"]);
        int workstationId = Convert.ToInt32(keys["WorkstationId"]);

        try
        {
            Facade.Employee().DeleteWorkstation(companyId, workstationId);
        }
        catch (SubmitException)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að eyða.');", true);
        }
        ListWorkstations();
        
	}

    protected void _dtgWorkstation_RowEditing(object sender, GridViewEditEventArgs e)
    {
        System.Collections.Specialized.OrderedDictionary keys =
            (System.Collections.Specialized.OrderedDictionary)_dtgWorkstation.DataKeys[e.NewEditIndex].Values;

        //int companyId = Convert.ToInt32(keys["CompanyId"]);
        int workstationId = Convert.ToInt32(keys["WorkstationId"]);

        Response.Redirect(string.Format("~/NewEntryWorkstation.aspx?Change=true&WorkstationId={0}", workstationId));
    }
    #endregion

    #region Link Transfers
    protected void _btnNewAction_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("~/NewEntryAction.aspx"));
    }
    protected void _btnNewWorkstation_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("~/NewEntryWorkstation.aspx"));
    }

    protected void _lbtnOpenHours_Click(object sender, EventArgs e)
    {
		Server.Transfer("~/OpenHours.aspx",true); 
    }
    protected void _lbtnTakeTime_Click(object sender, EventArgs e)
    {
		Server.Transfer("~/TakeVacation.aspx",true);
    }
	protected void _ibmHomePage_Click(object sender, ImageClickEventArgs e)
	{
        if (!IsPostBack || (e.X != 0 && e.Y != 0))
        {
            Server.Transfer("~/Company.aspx",true);
        }
	}
	protected void _lbtnCompanyBooking_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/CompanyBooking.aspx",true);
	}
    protected void _lbtnNewEntryCompany_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/NewEntryCompany.aspx?Change=true", true);
    }

	protected void _lbtnLogin_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/Login.aspx", true);
	}

    #endregion

    #region Private List Methods
    private void ListWorkstations()
    {
        _dtgWorkstation.DataSource = Facade.Anonymous().GetWorkstations(GetCompanyIdFromCookie());
        _dtgWorkstation.DataBind();
    }

    private void ListActivity()
    {
        _dtgAction.DataSource = Facade.Anonymous().GetActivities(GetCompanyIdFromCookie());
        _dtgAction.DataBind();
    }
    #endregion
}
